<?php
namespace jy_163;
defined('BY_JYA') or exit('error');
/*
	推送文章机制
*/
class set_data extends \plugin{
	public function upnews($title='',$content='',$user_classify='',$pic_url=''){
		$setting=$this->plugin_setting('setting');
		$access_token=$setting['access_token'];
		$client_key=$setting['client_id'];
		if(!$access_token){
			exi('access_token未获取，需移步站点宣传--多媒体--网易号--设置--授权获取access_token！','error');
		}
		if(!$client_key){
			exi('client_key未填写','error');
		}
		$url = 'http://mp.163.com/wemedia/article/status/api/oauth2/publish.do?access_token='.$access_token.'&client_id='.$client_key; //发布图文接口的URL
		$post=array('title'=>$title,'content'=>$content,'user_classify'=>$user_classify,'cover'=>'custom','pic_url'=>$pic_url);
		$post=ijson_encode($post);
		$res = cfc('http')->ihttp_request($url,$post);
		$shuju=$res['content'];
		$message=$shuju['message'];
		$data=$shuju['data'];
		return $message;
	}
	public function getcategory(){
		$setting=$this->plugin_setting('setting');
		$access_token=$setting['access_token'];
		$client_key=$setting['client_id'];
		if(!$access_token){
			exi('access_token未获取，需移步站点宣传--多媒体--网易号--设置--授权获取access_token！','error');
		}
		if(!$client_key){
			exi('client_key未填写','error');
		}
		if(!$setting['client_secret']){
			exi('网易号的密钥未填写','error');
		}
		$url = "http://mp.163.com/oauth2/article/category/get.do?&client_secret=".$setting['client_secret']."&client_id=".$client_key; //发布图文接口的URL
		$post=array('from'=>'jinyun');
		$post=ijson_encode($post);
		$res = cfc('http')->ihttp_request($url,$post);
		$shuju=$res['content'];
		return $message;
	}
	public function getAccessToken($code){//获取access_token
		$setting=$this->plugin_setting('setting');
		$return['error']=0;
		if(!$setting['access_token'] || $setting['expire']<TIMESTAMP){
			$url = "https://mp.163.com/oauth2/access_token?client_id=".$setting['client_id']."&client_secret=".$setting['client_secret']."&grant_type=authorization_code&code=".$code;
			$post=array('from'=>'jinyun');
			$post=ijson_encode($post);
			$content = cfc('http')->ihttp_request($url,$post);
			$res['content'];
			if(is_error($content)){
				$return['error']=1;
				$return['message']='获取网易号授权失败, 请稍后重试！错误详情: ' . $content['message'];
			}
			$token = @json_decode($content['content'], true);
			if(empty($token) || empty($token['access_token'])) {
				$return['error']=1;
				$return['message']='获取网易号授权失败, 请稍后重试！ 公众平台返回原始数据为: 错误代码-' . $token['errcode'] . '，错误信息-' . $token['errmsg'];
			}
			$setting['access_token'] = $token['access_token'];
			$setting['expire'] = TIMESTAMP + 28*24*3600;
			$this->plugin_setting('setting',$setting);
		}
		$return['access_token']=$setting['access_token'];
		return $return;
	}
}